#include <OsgClassDataSource/ObjectOsg.h>
#include <osg/Node>
#include <iostream>
#include <EDesignerCore.h>
#include <QDateTime>
#include <FunctionsPluginOsg.h>
#include <EngineFunctions.h>
#include <osg/Group>
#include <NodeInfoContainer.h>
ObjectOsg::ObjectOsg( QObject *parent /*= 0*/ ) : EObject(parent)
{
    m_object = NULL;
    setEObjectName("Object-");
    //m_object->setName("osg_object_hello_world");

    this->refreshInfoContainer();

}

ObjectOsg::~ObjectOsg()
{
    //std::cout<<"I am delete"<<m_object->getName()<<std::endl;

    //remove Info container
    if(m_object.get() != NULL)
    {
        m_object->setUserDataContainer(NULL);
    }
}

void ObjectOsg::setName( const QString& node_name )
{
    FunctionsPluginOsg::EDesignerCoreSimulationWaitUpdateEndAndStopSim();
    if(m_object != NULL)
    {
        m_object->setName(node_name.toStdString());

    }
    setEObjectName(this->metaObject()->className() + QString("-") + node_name);

    osg::Group* groupPtr = dynamic_cast<osg::Group*>(m_object.get());
    if(groupPtr != NULL)
    {
        //std::cout<<"group child num ="<<groupPtr->getNumChildren()<<std::endl;
        //std::cout<<"group parent num ="<<groupPtr->getParents().size()<<std::endl;
    }

    FunctionsPluginOsg::EDesignerCoreSimulationStopWaitUpdateEnd();
}

QString ObjectOsg::getName()
{
    //std::cout<<"ObjectOsg::getName"<<std::endl;
    //return QString::fromStdString(m_object->getName());
    QString str;
    if(m_object != NULL)
    {
        str = QString::fromStdString(m_object->getName());
    }
    else
    {
        //QStringList strList = m_object.split("-");
        //str = strList.at(1);
    }
    return str;
}

osg::ref_ptr<osg::Object> ObjectOsg::getObject()
{
    return m_object;
}

void ObjectOsg::replaceOsgObject(osg::ref_ptr<osg::Object> object)
{
    //std::cout<<"ObjectOsg::replaceOsgObject m_object = "<<(int)m_object.get()<<std::endl;
    m_object.release();
    m_object = object;
    this->refreshInfoContainer();
    //std::cout<<"ObjectOsg::replaceOsgObject m_object = "<<(int)m_object.get()<<std::endl;

}

void ObjectOsg::refreshItemTest()
{
    if(m_object != NULL)
    {
        setEObjectName(this->metaObject()->className() + QString("-") + QString::fromStdString(m_object->getName()));

    }
    else
    {
        setEObjectName(this->metaObject()->className() + QString("-") );

    }

}

void ObjectOsg::copyFrom(EObject *objPtr)
{
    EObject::copyFrom(objPtr);
    ObjectOsg* objOsgPtr = dynamic_cast<ObjectOsg*>(objPtr);
    if(objOsgPtr == NULL)return;

}

EObject *ObjectOsg::clone()
{
    ObjectOsg* newObj = new ObjectOsg();
    newObj->copyFrom(this);
    return newObj;
}

void ObjectOsg::refreshInfoContainer()
{
    if(m_object.get() != NULL)
    {
        NodeInfoContainer* newInfoContainer = new NodeInfoContainer;
        newInfoContainer->setObjectInspectorItem( this->getObjItem() );

        m_object->setUserDataContainer(newInfoContainer);
    }
}

void *ObjectOsg::getRelevantPtr()
{
    return m_object.get();
}

//void ObjectOsg::copyFrom(EObject *objPtr)
//{

//}

//EObject *ObjectOsg::clone()
//{
//    ObjectOsg* new_object_qt = new ObjectOsg(*this);

//    osg::CopyOp copy_op(osg::CopyOp::Options::DEEP_COPY_ALL);
//    osg::ref_ptr<osg::Object> new_node = copy_op(this->m_object);

//    new_object_qt->replaceOsgObject(new_node);
//    return new_object_qt;
//}


